package com.yushu.base.controller;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.yushu.base.pojo.vo.LogVO;
import com.yushu.base.pojo.bo.Log;
import com.yushu.base.service.LogService;
import com.yushu.common.execption.CgException;
import com.yushu.common.anno.LogAop;
import com.yushu.common.shiro.ShiroUtil;
import com.yushu.common.util.pager.Pages;
import com.yushu.common.util.result.Result;

import org.apache.commons.lang3.StringUtils;

//import io.swagger.annotations.Api;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author test
 * @CreateDate 2020-12-03 15:47:25
 */
//@Api(description = "日志管理")
@Controller
@RequestMapping("/base/log")
public class LogController {
	private Logger logger = Logger.getLogger(LogController.class);

    @Autowired
    private LogService logService;
    private Map  <String, Object> resultMap;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值
    }
    /**
    * 分页条件查询日志数据
    * 2020-01-14 16:14:16
    * @param tablePage
    * @return
    */
    @LogAop("分页查询日志")
    @ResponseBody
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Pages page(@RequestBody Pages<LogVO> tablePage) {
        Pages page = logService.page(tablePage);
        return page;
    }
	/**
    * 查询全部日志数据
    * 2020-01-14 16:14:16
    */
    @ResponseBody
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    public List<Log> queryList(@RequestBody Log log) {
        List<Log> list = logService.selectList(log);
        return list;
    }
	/**
	* 添加或者更新日志
	*2020-01-14 16:14:16
	* @param log
	* @return
	*/
	@LogAop("更新或新增日志")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public  Result  save(@RequestBody Log log ) {
        try {
			if (!StringUtils.isEmpty(log.getId())) {
				logger.info("进入修改日志方法");
				logService.updateByKey(log);
                return Result.success("日志修改成功");
			} else {
				logger.info("进入添加日志方法");
				logService.insert(log);
                return Result.success("日志新增成功");
			}
	        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());

        }       
	}
	
    /**
    *删除日志数据
    *2020-01-14 16:14:16
    * @param log 需要删除的对象
    */
    @LogAop("删除日志")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(Model model,  @RequestBody Log log ) {
        try {
            logService.deleteById(log.getId());
	        logger.info("删除日志成功");
	        return Result.success("删除日志成功");
        } catch (CgException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
    *
    * 批量删除日志数据
    * 2020-01-14 16:14:16
    * @param logs 日志集合
    */
    @LogAop("批量删除日志")
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ResponseBody
    public Result batchDelete(Model model, @RequestBody List<Log> logs) {
        logger.info("进入到删除日志数据方法");
        try {
            logService.deleteBatch(logs);
            return Result.success("批量删除日志成功");
        } catch (CgException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());

        }
    }

    /**
    * 跳转日志列表页面
    * 2020-01-14 16:14:16
    */
    @RequestMapping(value = "toLogList", method = RequestMethod.GET)
    public String toLogList(Model model ) {
        model.addAttribute("title","日志管理");
        model.addAttribute("page","/base/log/list");
        return "menuPage";
    }

    /**
    * 跳转日志跳转到空白列表页面
    * 2020-01-14 16:14:16
    */
    @RequestMapping(value = "toOpenLogList", method = RequestMethod.GET)
    public String toOpenLogList(Model model,HttpServletRequest request) {
        model.addAttribute("title", "日志管理");
        model.addAttribute("page", "/base/log/list");
        return "openPage";
    }

    /**
    * 跳转日志新增或修改页面
    * 2020-01-14 16:14:16
    */
    @RequestMapping(value = "toEditor", method = RequestMethod.GET)
    public String toEditor(Model model, String id , HttpServletRequest request) {
        if(StringUtils.isNotEmpty(id)){
	        request.setAttribute("data",  logService.queryLogByKey(id));
        }
        model.addAttribute("title", "日志管理");
        model.addAttribute("page", "/base/log/addUpdate");
        return "openPage";
    }
    @RequestMapping("checkRepeat")
    @ResponseBody
    public String checkRepeat(@RequestBody Map map ) {
        return logService.checkRepeat(map);
    }    
}

